package com.zbcz.crm.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.zbcz.crm.RelatedAccessoriesActivity
import com.zbcz.crm.RelatedAccessoriesActivity.Companion.PRODUCT_ID
import com.zbcz.crm.bean.ProductBean
import com.zbcz.crm.databinding.ItemProductListBinding
import com.zbcz.crm.network.RetrofitManager
import com.zbcz.crm.sp.SharedPreferencesManager
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.util.setRoundedCorners

class ProductBeanListAdapter(val mDatas: List<ProductBean>) :
    RecyclerView.Adapter<ProductBeanListAdapter.ProductHolder>() {
    private val expandedStates = mutableMapOf<Int, Boolean>()

    inner class ProductHolder(private val binding: ItemProductListBinding) :
        ViewHolder(binding.root) {

        init {
            binding.inContent.tvAccessoriesQuery.onSingleClick {
                binding.root.context?.let { context: Context ->
                    context.startActivity(
                        Intent(
                            context,
                            RelatedAccessoriesActivity::class.java
                        ).apply {
                            putExtra(PRODUCT_ID, mDatas[adapterPosition].id)
                        })
                }
            }
        }

        fun bind(bean: ProductBean) {
            binding.inContent.run {
                tvProductNameValue.text = bean.productName
                tvProductTypeValue.text = bean.productType_dictText
                tvProductPurchasePriceValue.text = bean.purchasePrice.toString()
                tvProductGroupPriceValue.text = bean.groupPrice.toString()
                tvSellPriceValue.text = bean.sellingPrice.toString()
                tvProductIsOnShelvesValue.text = bean.isOn_dictText
                Glide.with(binding.root.context)
                    .load(RetrofitManager.getImageUrl(bean.productImg))
                    .into(ivProductImg)

            }
            binding.inContent.root.setRoundedCorners(45f)
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
        }

        fun setExpanded(expanded: Boolean) {
            val visible = if (expanded) View.VISIBLE else View.GONE
            binding.inContent.clProductPurchasePrice.visibility = visible
            binding.inContent.clProductGroupPrice.visibility = visible
            binding.inContent.clProductSellPrice.visibility = visible
            binding.inContent.clProductIsOnShelves.visibility = visible
            binding.inContent.clProductImg.visibility = visible
            binding.inContent.clAccessories.visibility = visible
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemProductListBinding.inflate(inflater, parent, false)
        return ProductHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: ProductHolder, position: Int) {
        holder.bind(mDatas[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }
}